package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.Category;
import com.lin.services.CategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:CategoryController
 * Package:com.lin.controller
 * Author: hellcat
 * Date: 2019/12/19
 * ♣♣♣  分类管理
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @MyLog("查询分类信息")
    @RequiresPermissions("wares:category:list")
    @RequestMapping(value = "/toCategory", method = RequestMethod.GET)
    public String toCategory(HttpServletRequest request) {
        request.setAttribute("ddId", "Category");
        return "categoryMain";
    }

    @RequiresPermissions("wares:category:list")
    @RequestMapping(value = "/getAllCategory", method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String, Object>> getAllCategory() {
        return categoryService.getAllCategory();
    }

    @MyLog("添加分类")
    @RequiresPermissions("wares:category:insert")
    @RequestMapping(value = "/insertCategory", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertCategory(HttpServletRequest request) {
        String parentCategoryId = request.getParameter("parentCategoryId");
        String categoryName = request.getParameter("categoryName");
        return categoryService.insertCategory(parentCategoryId, categoryName);
    }


    @MyLog("修改分类信息")
    @RequiresPermissions("wares:category:update")
    @RequestMapping(value = "/editCategory", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateCategory(HttpServletRequest request) {
        String parentCategoryId = request.getParameter("parentId");
        String categoryId = request.getParameter("nodeId");
        String categoryName = request.getParameter("editCategoryName");
        return categoryService.updateCategory(parentCategoryId, categoryId, categoryName);
    }

    @MyLog("删除分类")
    @RequiresPermissions("wares:category:delete")
    @RequestMapping(value = "/delCategory", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteCategory(HttpServletRequest request) {
        String parentCategoryId = request.getParameter("parentId");
        String categoryId = request.getParameter("nodeId");
        return categoryService.deleteCategory(parentCategoryId, categoryId);
    }

}
